import cn.neu.aimp.iot.constant.Constant;
import cn.neu.aimp.iot.entity.Device;
import cn.neu.aimp.iot.entity.RealplayConfig;
import cn.neu.aimp.iot.module.LoginAndOutModule;
import cn.neu.aimp.iot.module.RealPlayModule;
import cn.neu.aimp.iot.storage.ImgStorage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Demo3 {
    static boolean flag = true;
    static boolean flag2 = true;

    public static void main(String[] args) throws IOException, InterruptedException {
        Device decice = LoginAndOutModule.login(Constant.HAIKANG, "192.168.1.61", 8000, "admin", "abc123..", null);
        RealplayConfig realplayConfig = new RealplayConfig();

        realplayConfig.setIsImgStream(1);
        Object[] objects = RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
        ImgThread imgThread = new ImgThread((ImgStorage) objects[0]);
        imgThread.start();

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsAutoControl(1);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsImgStream(0);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
        flag = false;
        RealPlayModule.snapshot(decice.getDeviceId(), "E:\\test\\ABCD.jpg");

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsAutoControl(0);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsImgStream(1);
        realplayConfig.setIsAutoControl(1);
        ImgThread2 imgThread2 = new ImgThread2((ImgStorage) objects[0]);
        imgThread2.start();
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
        RealPlayModule.snapshot(decice.getDeviceId(), "E:\\test\\ABCD.jpg");

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsAutoControl(0);
        realplayConfig.setIsImgStream(0);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
        flag2 = false;

        Thread.sleep(10000);
        realplayConfig = new RealplayConfig();
        realplayConfig.setIsAutoControl(1);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);

        Thread.sleep(10000);
        System.out.println("123");
        LoginAndOutModule.logout(decice.getDeviceId());
    }


    static class ImgThread extends Thread {
        ImgStorage imgStorages;

        public ImgThread(ImgStorage imgStorages) {
            this.imgStorages = imgStorages;
        }

        @Override
        public void run() {
            File f;
            for (int i = 0; flag; i++) {
                if (i % 3000 == 0) {
                    f = new File("E:\\test\\" + i + "MyFile.png");
                    BufferedImage image = imgStorages.consumePendingImg();
                    try {
                        ImageIO.write(image, "PNG", f);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class ImgThread2 extends Thread {
        ImgStorage imgStorages;

        public ImgThread2(ImgStorage imgStorages) {
            this.imgStorages = imgStorages;
        }

        @Override
        public void run() {
            File f;
            for (int i = 0; flag2; i++) {
                if (i % 3000 == 0) {
                    f = new File("E:\\test\\" + i + "MyFile1.png");
                    BufferedImage image = imgStorages.consumePendingImg();
                    try {
                        ImageIO.write(image, "PNG", f);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

